var $editForm = $("#editForm");

$editForm.delegate('.js-submit', 'click', function() {
    if (!confirm("确定要提交修改？")) {
        return false;
    }
    var url = BASE_PATH + '/bg/role/resource'
    var param = serializeArrayToJson($editForm.serializeArray());
    $.post(url, param, function(resp) {
        if (resp.code != 0) {
            doAlert('操作失败！');
            return false;
        }
        doAlert('修改成功！', 1500, 'success', function() {
            window.location.href = "./list"
        })
    })
})

$editForm.delegate(':checkbox[name=resource]', 'change', function() {
    var $parents = $(this).parents('.js-line');
    var total = $(':checkbox[name=resource]', $parents).length;
    var selectCount = $(':checkbox[name=resource]:checked', $parents).length;
    $(':checkbox[name=all]', $parents).prop('checked', total == selectCount);
})

$editForm.delegate(':checkbox[name=all]', 'change', function() {
    var $parents = $(this).parents('.js-line');
    var isAll = $(this).is(':checked');
    $(':checkbox[name=resource]', $parents).prop('checked', isAll);
})

function modifyAll() {
    $('.js-line').each(function() {
        var $parents = $(this);
        var total = $(':checkbox[name=resource]', $parents).length;
        var selectCount = $(':checkbox[name=resource]:checked', $parents).length;
        $(':checkbox[name=all]', $parents).prop('checked', total == selectCount);
    })
}
$(document).ready(function() {
    modifyAll();
})